﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Billing.CashDesk;
using Billing.CashDesk.AuthenticationService;

namespace Billing.CashDesk
{
    public partial class PassChangeForm : System.Windows.Forms.Form
    {
        private bool mNewPassword = false;
        public PassChangeForm()
            : this(false)
        {
        }

        public PassChangeForm(bool newPassword)
        {
            InitializeComponent();
            if (newPassword)
            {
                mNewPassword = newPassword;
                this.oldPassTxt.Enabled = false;
            }
        }

        private void okUltraButton_Click(object sender, EventArgs e)
        {
            if ((oldPassTxt.Text == String.Empty && mNewPassword == false) || newPassTxt.Text == String.Empty || confirmPassTxt.Text == String.Empty)
                return;
            if (String.Equals(newPassTxt.Text, confirmPassTxt.Text) == false)
            {
                MessageBox.Show("გამეორებული პაროლი არასწორია!");
                confirmPassTxt.Text = String.Empty;
                confirmPassTxt.Select();
                return;
            }
            else
            {
                IAuthenticationService service = ServiceFactory.CreateAuthenticationService();
                bool? result = false;
                CryptoUtils cryptoUtils = new CryptoUtils();
                service.ChangeUserPassword((int?)Profile.UserID, cryptoUtils.GetMD5(oldPassTxt.Text), cryptoUtils.GetMD5(newPassTxt.Text), ref result);
                if ((bool)result)
                {
                    MessageBox.Show("პაროლი შეიცვალა!");
                }
                else
                {
                    MessageBox.Show("ძველი პაროლი არასწორადაა შეყვანილი!");
                    oldPassTxt.Text = newPassTxt.Text = confirmPassTxt.Text = String.Empty;
                    oldPassTxt.Select();
                    return;
                }
            }
            DialogResult = DialogResult.OK;
            Close();
        }
    }
}
